package org.jeecg.modules.flower.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.flower.entity.FlowerOutorder;
import org.jeecg.modules.flower.service.*;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.handle.enums.SendMsgStatusEnum;
import org.jeecg.modules.message.service.ISysMessageService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 *72小时运管自动确认
 * @descriptionauthor
 * @author gf
 * 2022-9-27 15:20
 *
 * @return
 */
@Slf4j
public class AutoYunGuanSureJob implements Job {
	@Autowired
	private IFlowerOutorderService flowerOutorderService;

	@Autowired
	private IFlowerOutorderinfoService flowerOutorderinfoService;

	@SneakyThrows
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		log.info(String.format(" 72小时运管自动确认 !  时间:" + DateUtils.getTimestamp()));
		log.info(String.format(" 72小时运管自动确认 !  时间:" + DateUtils.getDate()));
		log.info( "  =========  start  ===========>>       "  );

		//计算72小时之前的时间。
		Calendar calendar =Calendar.getInstance();
		calendar.add(Calendar.HOUR,-72);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = simpleDateFormat.format(calendar.getTime());

		//查询确认出库时间 小于当前的 72小时的单据。

		LambdaQueryWrapper<FlowerOutorder> queryWrapper =new LambdaQueryWrapper<>();
		queryWrapper.eq(FlowerOutorder::getState, 0) ;
		queryWrapper.isNotNull(FlowerOutorder::getSureouttime);
		queryWrapper.le(FlowerOutorder::getSureouttime,format ) ;

		List<FlowerOutorder> list = flowerOutorderService.list(queryWrapper);
		if(list.size()>0){
			for(FlowerOutorder flowerOutorder : list){
				flowerOutorderService.transportPipe(flowerOutorder.getId(),flowerOutorder.getRemarks(), null , null);
				log.info(format+ "  ========执行一条============      "  + flowerOutorder.getId());
			}
		}
		log.info( "  ==========  end  ==========       "  );
	}

}
